package com.yunxi.service.localService.sys.impl;

import com.github.pagehelper.PageInfo;
import com.yunxi.core.page.PageHelperPlus;
import com.yunxi.core.service.ServiceImplPlus;
import com.yunxi.core.util.WgrDate;
import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.LoginInfo;
import com.yunxi.core.web.ResponseUtil;
import com.yunxi.dao.sys.StaffMapper;
import com.yunxi.model.sys.Staff;
import com.yunxi.model.sys.User;
import com.yunxi.service.localService.sys.StaffService;
import com.yunxi.service.localService.sys.UserService;
import com.yunxi.sys.pojo.RoleVO;
import com.yunxi.sys.pojo.StaffListVO;
import com.yunxi.sys.pojo.StaffVO;
import com.yunxi.sys.pojo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 员工信息 服务实现类
 * </p>
 *
 * @author 
 * @since 2019-05-20
 */
@Service
@Qualifier("staffServiceImpl")
public class StaffServiceImpl extends ServiceImplPlus<StaffMapper, Staff> implements StaffService {

    @Autowired
    @Qualifier(value = "userServiceImpl")
    UserService userService;

    /**
     * 通过组织id获取该组织下人员
     * @param orgId
     * @return
     */
    @Override
    public List<StaffListVO> getStaffListById(String orgId) {
        List<StaffListVO> list = baseMapper.getStaffListById(orgId);
        return list;
    }

    @Override
    public StaffVO getByUserCode(String userCode) {
        Map map = new HashMap();
        map.put("userCode",userCode);
        return baseMapper.getByUserCode(map);
    }

    @Override
    public StaffVO getCasById(String staffId) {
        return baseMapper.getCasById(staffId);
    }

    @Override
    public List<RoleVO> listRole(String tenantId) {
        return baseMapper.listRole(tenantId);
    }

    @Override
    public void syncStaff(Staff staff) {
        if(!staff.getStaffId().equals(""))
            this.saveOrUpdate(staff);
        UserVO user = userService.getByUserCode(staff.getAccountNo());
        if(user==null){
            User user_ = new User();
            user_.setUserName(staff.getStaffName());
            user_.setRealName(staff.getStaffName());
//            user_.setCompanyId(staff.getOrgId());
            user_.setUserCode(staff.getAccountNo());
            user_.setUserType(1);
            user_.setStatus(1);
            user_.setIsOpen(0);
            user_.setCreateDate(WgrDate.getCurrentDate());
            userService.save(user_);
        }else{
            if(staff.getStatus()==0){
                User u2= new User();
                u2.setStatus(0);
                u2.setUserId(user.getUserId());
                userService.updateById(u2);
            }
        }
    }

    @Override
    public void batchCreateUser(String flag) throws InvocationTargetException, IllegalAccessException {
        //查询没有创建user的staff
        Map map = new HashMap();
        map.put("noUser","1");
        List<StaffVO> list = baseMapper.queryByList(map);

        for (int i=0;i < list.size() ; i++ ){
            StaffVO staffv = list.get(i);
            Staff staff = new Staff();
            BeanUtils.copyProperties(staffv,staff);
            syncStaff(staff);
        }
    }

   @Override
   public List<StaffVO> listByPartnerCode(String partnerCode) {

      return baseMapper.listByPartnerCode(partnerCode);
   }

    @Override
    public List<StaffVO> listManagerByPartnerCode(String partnerCode) {
        return baseMapper.listManagerByPartnerCode(partnerCode);
    }

   @Override
   public PageInfo listRegionManager(String areaCode,Integer pageNum, Integer pageSize) {
      PageHelperPlus.startPage(pageNum,pageSize);
      List<StaffVO> list = baseMapper.listRegionManager(areaCode);
      PageInfo<StaffVO> pageInfo = new PageInfo<StaffVO>(list);
      return pageInfo;
   }

   @Override
   public List<StaffVO> listRegionManagerByAreaCode(String areaCode) {
      return baseMapper.listRegionManager(areaCode);
   }

    @Override
    public PageInfo listAllRegionManager(Integer pageNum, Integer pageSize) {
       PageHelperPlus.startPage(pageNum,pageSize);
       List<StaffVO> list = baseMapper.listAllRegionManager();
       PageInfo<StaffVO> pageInfo = new PageInfo<StaffVO>(list);
       return pageInfo;
    }

   @Override
   public PageInfo listRegionManagerNotExits(String areaCode, Integer pageNum, Integer pageSize) {
      PageHelperPlus.startPage(pageNum,pageSize);
      List<StaffVO> list = baseMapper.listRegionManagerNotExits(areaCode);
      PageInfo<StaffVO> pageInfo = new PageInfo<StaffVO>(list);
      return pageInfo;
   }

   @Override
   public ApiResult listStaffByRoleCode(String roleCode, LoginInfo loginInfo) {
      String partnerCode = loginInfo.getPartnerCode();
      List<StaffVO> staffVOS = baseMapper.listStaffByRoleCode(partnerCode, roleCode);
      return ResponseUtil.ok(staffVOS);

   }

   @Override
   public List<StaffVO> getRegionManagerByUserCode(String fellowId) {
      return baseMapper.getRegionManagerByUserCode(fellowId);
   }

   @Override
   public PageInfo listAllCityManager(Integer pageNum, Integer pageSize) {
      PageHelperPlus.startPage(pageNum,pageSize);
      List<StaffVO> list = baseMapper.listAllCityManager();
      PageInfo<StaffVO> pageInfo = new PageInfo<StaffVO>(list);
      return pageInfo;
   }

   @Override
   public PageInfo listCityManagerNotExits(Integer pageNum, Integer pageSize) {
      PageHelperPlus.startPage(pageNum,pageSize);
      List<StaffVO> list = baseMapper.listCityManagerNotExits();
      PageInfo<StaffVO> pageInfo = new PageInfo<StaffVO>(list);
      return pageInfo;
   }

   @Override
   public PageInfo listCityManagerByAccountNo(String accountNo, Integer pageNum, Integer pageSize) {
      PageHelperPlus.startPage(pageNum,pageSize);
      List<StaffVO> list = baseMapper.listCityManagerByAccountNo(accountNo);
      PageInfo<StaffVO> pageInfo = new PageInfo<StaffVO>(list);
      return pageInfo;
   }

   @Override
   public List<StaffVO> listCityManagerByCityCode(String cityCode) {
      List<StaffVO> list = baseMapper.listCityManagerByCityCode(cityCode);
      return list;
   }

    /**
     * 根据用户编号,删除销售目标记录
     * @param accountNo
     */
    @Override
    public void removeTargetByAccountNo(String accountNo) {
        baseMapper.removeTargetByAccountNo(accountNo);
    }
}
